import { request } from "../request";

const token = localStorage.getItem("token");

// 数据接口定义
export interface ClassTeachers {
    id?: number;
    classId?: number;
    teacherId?: number;
}

export interface Classes {
    classId?: number;
    name?: string;
    description?: string;
    createdAt?: Date;
}

export interface ClassStudents {
    id?: number;
    classId?: number;
    studentId?: number;
}

export interface ResponseEntity<T> {
    code: number;
    message: string;
    data: T;
    success: boolean;
}

// 创建班级
const CreateClassAPI = async (
    name: string, 
    description?: string, 
    teacherId?: number
): Promise<ResponseEntity<string>> => {
    let data = {
        className: name,
        description: description,
        teacherId: teacherId
    };
    if (description) {
        data.description = description;
    }
    const token = localStorage.getItem("token");
    return request("/api/class-teachers/createClass", {

        method: "POST",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            'Authorization': `Bearer ${token}`
        },
        data: data
    });
};

// 删除班级
const deleteClassAPI = async (classTeachers: ClassTeachers): Promise<ResponseEntity<string>> => {
    const token = localStorage.getItem("token");
    return request("/api/class-teachers/deleteClass", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            'Authorization': `Bearer ${token}`
        },
        data: classTeachers
    });
};

// 获取所有班级信息
const getAllClassesByTeachersIdAPI = async (classTeachers: ClassTeachers): Promise<ResponseEntity<Classes[]>> => {
    const token = localStorage.getItem("token");
    return request("/api/class-teachers/getAllClassesByTeachersId", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            'Authorization': `Bearer ${token}`
        },
        data: classTeachers
    });
};

// 通过班级ID邀请学生加入班级
const inviteStudentByIdAPI = async (classId: number, studentId: number): Promise<ResponseEntity<string>> => {
    const token = localStorage.getItem("token");
    return request("/api/class-teachers/inviteStudentById", {
        method: "POST",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            'Authorization': `Bearer ${token}`
        },
        data: {
            classId: classId,
            studentId: studentId
        }
    });
};

// 通过邀请码邀请学生加入班级
const inviteStudentByInviteCodeAPI = async (classname: string): Promise<ResponseEntity<string>> => {
    const token = localStorage.getItem("token");
    return request("/api/class-teachers/inviteStudentByInviteCode", {
        method: "POST",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            'Authorization': `Bearer ${token}`
        },
        data: {
            classname: classname
        }
    });
};

// 获取班级学生信息
const getClassesStudentByClassIdAPI = async (classId: number): Promise<ResponseEntity<ClassStudents[]>> => {
    const token = localStorage.getItem("token");
    return request("/api/class-teachers/getClassesStudentByClassId", {
        method: "POST",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            'Authorization': `Bearer ${token}`
        },
        data: {
            classId: classId
        }
    });
};

// 导出所有API
export {
    CreateClassAPI,
    deleteClassAPI,
    getAllClassesByTeachersIdAPI,
    inviteStudentByIdAPI,
    inviteStudentByInviteCodeAPI,
    getClassesStudentByClassIdAPI
};
